package cn.yunhe.controller;

import cn.yunhe.entity.Member;
import cn.yunhe.entity.Orders;
import cn.yunhe.entity.Product;
import cn.yunhe.entity.Traveller;
import cn.yunhe.service.MemberService;
import cn.yunhe.service.OrderService;
import cn.yunhe.service.ProductService;
import cn.yunhe.service.TravellerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private TravellerService travellerService;
    @Autowired
    private ProductService productService;
    @Autowired
    private MemberService memberService;

    @RequestMapping("/findAll")
    public String findAll(Model model,
                          @RequestParam(value = "page", defaultValue = "1") Integer page,
                          @RequestParam(value = "size", defaultValue = "5") Integer size) {
        PageHelper.startPage(page, size);
        List<Orders> ordersList = orderService.findAll();
        ordersList.forEach(System.out::println);
        PageInfo<Orders> ordersPageInfo = new PageInfo<>(ordersList);

        model.addAttribute("pageInfo", ordersPageInfo);
        return "order-list";
    }

    @RequestMapping("/add")
    public String add(Model model) {
        List<Traveller> travellerList = travellerService.findAll();
        List<Product> list = productService.findAll();
        List<Member> memberList = memberService.findAll();
        model.addAttribute("productList", list);
        model.addAttribute("travellerList", travellerList);
        model.addAttribute("memberList", memberList);
        return "order-add";
    }

    @RequestMapping("/save")
    public String save(Orders orders) {
        orderService.insertOrders(orders);

        return "redirect:findAll";
    }

    @RequestMapping("/deleteById")
    public String deleteById(Integer id) {
        orderService.deleteById(id);
        return "redirect:findAll";
    }

    @RequestMapping("/editById")
    public String editById(Integer id, Model model) {
        Orders orders = orderService.selectById(id);
        model.addAttribute("orders", orders);

        return "order-edit";
    }

    @RequestMapping("edit")
    public String edit(Orders orders) {
        orderService.updateById(orders);
        return "redirect:findAll";
    }

    @RequestMapping("/findById")
    public String findById(Integer id, Model model) {
        Orders orders = orderService.findById(id);
        System.out.println(orders);
        model.addAttribute("order", orders);
        return "order-show";
    }

    @RequestMapping("/selectDelete")
    public String selectDelete(Integer[] ids) {
        orderService.selectDelete(ids);
        return "redirect:findAll";
    }

}
